package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.HashMap;

import static java.lang.System.out;

/**
 * Created by michaelbottjer on 16/04/2014.
 */
public class Library {

    ArrayList<Book> availableBooks;
    ArrayList<Movie> availableMovies;
    HashMap<String, String> currentlyCheckedOutItems;

    public Library(){
        createDefaultBookArray();
        createDefaultMovieArray();
        currentlyCheckedOutItems = new HashMap<String, String>();
    }

    private void createDefaultMovieArray() {
        availableMovies = new ArrayList<Movie>();
        addDefaultMovies(availableMovies);
    }

    private void addDefaultMovies(ArrayList<Movie> listOfMovies) {
        listOfMovies.add(new Movie("Wolf of Wall Street", "Martin Scorsese", 2013));
        listOfMovies.add(new Movie("2001: A Space Odyssey", "Stanley Kubrick", 1968, "9"));
        listOfMovies.add(new Movie("The Shawshank Redemption", "Frank Darabont", 1994, "10"));
    }

    private void createDefaultBookArray(){
        ArrayList<Book> listOfBooks = new ArrayList<Book>();
        addDefaultBooks(listOfBooks);
        availableBooks = listOfBooks;
    }

    private void addDefaultBooks(ArrayList<Book> listOfBooks){
        listOfBooks.add(new Book("TDD"));
        listOfBooks.add(new Book("BDD"));
        listOfBooks.add(new Book("Extreme Programming"));
    }


    public static <T extends Item> ArrayList<String> listAvailableTitles(ArrayList<T> listOfItems){
        ArrayList<String> listOfAvailableTitles = new ArrayList<String>();
        for(Item item : listOfItems){
            listOfAvailableTitles.add(item.getTitle());
        }
        return listOfAvailableTitles;
    }

    public <T extends Item> void addToCheckedOutList(Customer customer, int number, ArrayList<T> itemList){
        currentlyCheckedOutItems.put(itemList.get(number-1).getTitle(), customer.details.get("name"));
    }

    public <T extends Item> void removeFromCheckedOutList(int number, ArrayList<T> itemList){
        currentlyCheckedOutItems.remove(itemList.get(number-1).getTitle());
    }

    public void locationOfItems() {
        for( String key : currentlyCheckedOutItems.keySet()){
            out.println(key + " " + "-" + " " + currentlyCheckedOutItems.get(key));
        }
    }

}
